package com.epfly.base.id;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

@Slf4j
@Configuration
public class SnowflakeConfiguration {

    private static final String key = "c:e:i:p:pk";

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    private final long mask = -1L ^ (-1L << 10); // 1023

    @Bean
    public SnowflakeIdService createSnowflakeIdWorkerService() {

        Long val = redisTemplate.opsForValue().increment(key);
        val = val & mask; // val范围(0~1023)

        return new SnowflakeIdWorkerServiceImpl(val);
    }

}